// function timeout(ms){
// return new Promise((resolve)=>{
//     setTimeout(resolve,ms)
// })
// }

const timeout=(ms)=>{
    return new Promise((resolve)=>{
            setTimeout(resolve,ms)
    })
}

async function timeou2(ms){
    await new Promise(resolve=>{
            setTimeout(resolve,ms)
    })
}

async function test(val,ms){
    //  await timeout(ms);
     await timeou2(ms)
     console.log(val)
}
test('你好啊！',1000)


async function fo(){
    return 'he  word';
}
fo().then(v=>{
    console.log(v)
})//上面代码中，函数f内部return命令返回的值，会被then方法回调函数接收到。 函数内部return语句返回的值，会成为then方法回调函数的参数

var fo2 = async function(){
    throw new Error('出错了')
}
fo2().then(
    v=>{console.log(v)},
    e=>{console.log(e)}
)

function resolveAfter2Seconds(x) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(x);
      }, 2000);
    });
  };
  
  // async function expression assigned to a variable
  var add1 = async function(x) {
    var a = await resolveAfter2Seconds(20);
    var b = await resolveAfter2Seconds(30);
    return x + a + b;
  }
  
  add1(10).then(v => {
    console.log(v);  // 4 秒后打印 60
  });
  
  (async function(x) { // async function expression used as an IIFE
    var p_a = resolveAfter2Seconds(20);
    var p_b = resolveAfter2Seconds(30);
    return x + await p_a + await p_b;
  })(10).then(v => {
    console.log(v);  // 2 秒后打印 60
  });

//   let resolveAfter2Seconds1=function(x) { //报错 未知
//     return new Promise(resolve => {
//       setTimeout(() => {
//         resolve(x);
//       }, 2000);
//     });
//   }
// var AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
// var as = new AsyncFunction('a', 
//                           'b',
//                           'return await resolveAfter2Seconds1(a) + await resolveAfter2Seconds1(b);');
// as(10, 20).then(v => {
//   console.log(v); // 4 秒后打印 30
// });


